import React from 'react'
import axios from 'axios'
import { withRouter } from 'react-router-dom'
import { connect } from 'react-redux'
import { actionCreator } from '../../store/user/index';

class AutoRoute extends React.Component {
  componentDidMount() {
    const publicList = ['/login', '/register']
    const pathName = this.props.location.pathname
    if (publicList.indexOf(pathName) > -1) {
      return null
    }
    axios.get('/user/info').then(res => {
      if (res.status === 200) {
        if (res.data.code === 0) {
          //登录成功
          this.props.loadinfo(res.data.data)
          console.log(res.data.data)
        } else {
          this.props.logout()
          this.props.history.push('/login')
        }
      }
    })
  }
  render() {
    return null
  }
}
const mapDispatch = (dispatch) => ({
  loadinfo(data) {
    dispatch(actionCreator.loadinfo(data))
  },
  logout(data) {
    dispatch(actionCreator.logout())
  }
})
export default connect(null, mapDispatch)(withRouter(AutoRoute))